package com.ld.hpfs.web.controller;


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.dao.UtilMapper;
import com.ld.hpfs.pojo.ExtPager;
import com.ld.hpfs.pojo.User;
import com.ld.hpfs.service.RefTitleService;
import com.ld.hpfs.web.interseptor.WebConstants;

@Controller
public class RefTitleController {
	private static final Logger logger = LoggerFactory.getLogger(RefTitleController.class);

	@Autowired
	private RefTitleService refTitleService;
	
	@Autowired
    private UtilMapper   utilMapper;
	
	
	/**
	 * 登录时获取参考字段
	 * 2013.06.05 BY guxiaoli
	 */
	@RequestMapping(value = "/getRef")
    public void getTcode(ExtPager page, HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer) {       	   
        response.setContentType("text/plain;charset=UTF-8");
        HashMap<String, Object> result = new HashMap<String, Object>();
        try {       	          	       	         
             result.put("success", true);
        	 String company_id = session.getAttribute(WebConstants.CURRENT_COMPANY).toString();           
             String logs[] =refTitleService.getRef(company_id);            
                if (logs != null && logs.length > 0) {
                    result.put("data", logs);                  
                } else {
                    result.put("data", "");
                }	           
            String returnMsg = JackJson.fromObjectToJson(result);
            logger.debug("{}", returnMsg);
            writer.print(returnMsg);
        } catch (Exception e) {
            logger.error("Exception: {}", e);
        } finally {
            writer.flush();
            writer.close();
        }
    }

	/*
	 * setRefTitle重置参考字段名称
	 */
	@RequestMapping(value = "/setRefTitle",method = RequestMethod.POST)
    public void setTcodeTitle(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer) {
         String ref1  = (String)request.getParameter("ref1")  == null ? null : (String)request.getParameter("ref1");
         String ref2  = (String)request.getParameter("ref2")  == null ? null : (String)request.getParameter("ref2");
         String ref3  = (String)request.getParameter("ref3")  == null ? null : (String)request.getParameter("ref3");
         String ref4  = (String)request.getParameter("ref4")  == null ? null : (String)request.getParameter("ref4");
         String ref5  = (String)request.getParameter("ref5")  == null ? null : (String)request.getParameter("ref5");
         String ref6  = (String)request.getParameter("ref6")  == null ? null : (String)request.getParameter("ref6");
         String ref7  = (String)request.getParameter("ref7")  == null ? null : (String)request.getParameter("ref7");
         String ref8  = (String)request.getParameter("ref8")  == null ? null : (String)request.getParameter("ref8");
         String ref9  = (String)request.getParameter("ref9")  == null ? null : (String)request.getParameter("ref9");
         String ref10  = (String)request.getParameter("ref10")  == null ? null : (String)request.getParameter("ref10");
         String ref11  = (String)request.getParameter("ref11")  == null ? null : (String)request.getParameter("ref11");
         String ref12  = (String)request.getParameter("ref12")  == null ? null : (String)request.getParameter("ref12");
         String ref13  = (String)request.getParameter("ref13")  == null ? null : (String)request.getParameter("ref13");
         String ref14  = (String)request.getParameter("ref14")  == null ? null : (String)request.getParameter("ref14");
         String ref15  = (String)request.getParameter("ref15")  == null ? null : (String)request.getParameter("ref15");
         String ref16  = (String)request.getParameter("ref16")  == null ? null : (String)request.getParameter("ref16");
         String ref17  = (String)request.getParameter("ref17")  == null ? null : (String)request.getParameter("ref17");
         String ref18  = (String)request.getParameter("ref18")  == null ? null : (String)request.getParameter("ref18");
         String ref19  = (String)request.getParameter("ref19")  == null ? null : (String)request.getParameter("ref19");
         String ref20  = (String)request.getParameter("ref20")  == null ? null : (String)request.getParameter("ref20");
        response.setContentType("text/plain;charset=UTF-8");
        HashMap<String, Object> result = new HashMap<String, Object>();
        try {
           	 HashMap<String,Object> param = new HashMap<String,Object>();
	         param.put("ref1", ref1);
	         param.put("ref2", ref2);
	         param.put("ref3", ref3);
	         param.put("ref4", ref4);
	         param.put("ref5", ref5);
	         param.put("ref6", ref6);
	         param.put("ref7", ref7);
	         param.put("ref8", ref8);
	         param.put("ref9", ref9);
	         param.put("ref10", ref10);
	         param.put("ref11", ref11);
	         param.put("ref12", ref12);
	         param.put("ref13", ref13);
	         param.put("ref14", ref14);
	         param.put("ref15", ref15);
	         param.put("ref16", ref16);
	         param.put("ref17", ref17);
	         param.put("ref18", ref18);
	         param.put("ref19", ref19);
	         param.put("ref20", ref20);
	         param.put("companyId", session.getAttribute(WebConstants.CURRENT_COMPANY).toString());
	         HashMap<String,Object> check = refTitleService.checkRefCompany(param);
	         int ret;
	         if(check == null){
               ret =refTitleService.addRefTitle(param);
             }else{
               ret =refTitleService.editRefTitle(param); 
             }
             if(ret == 0){
            	 result.put("resultcode",0);
             }else{
            	 result.put("resultcode",9);
             }
            String returnMsg = JackJson.fromObjectToJson(result);
            logger.debug("{}", returnMsg);
            writer.print(returnMsg);
        } catch (Exception e) {
            logger.error("Exception: {}", e);
        } finally {
            writer.flush();
            writer.close();
        }
    }
	
}